home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / OGLT / Examples / TerrainFollowing / src.hansen / Main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  12.0 KB  |  400 lines

  1.  /**************************************************************************
  2.   *                                       *
  3.   *   Copyright (C) 1996, Silicon Graphics, Inc.               *
  4.   *              All Rights Reserved.                   *
  5.   *                                       *
  6.   *  The files in this subtree  contain  UNPUBLISHED  PROPRIETARY  SOURCE  *
  7.   *  CODE of Silicon Graphics, Inc.;  the  contents  of  these  files may  *
  8.   *  not be disclosed to third  parties,  copied  or  duplicated  in  any  *
  9.   *  form, in whole or in part,  without  the  prior  written  permission  * 
  10.   *  of  Silicon Graphics, Inc.                           *
  11.   *                                       *
  12.   *  RESTRICTED RIGHTS LEGEND:                           *
  13.   *  Use,  duplication  or  disclosure  by  the  Government is subject to  *
  14.   *  restrictions as set forth in subdivision (c)(1)(ii) of the Rights in  *
  15.   *  Technical Data and Computer Software clause at  DFARS  252.227-7013,  *
  16.   *  and/or in similar or successor  clauses in the FAR,  DOD or NASA FAR  *
  17.   *  Supplement.  Unpublished - rights reserved  under the Copyright Laws  *
  18.   *  of the United States.                           *
  19.   *                                       *
  20.   *  THIS SOFTWARE IS  PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,  *
  21.   *  EXPRESS,  IMPLIED OR  OTHERWISE,  INCLUDING  WITHOUT LIMITATION, ANY  *
  22.   *  WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.      *
  23.   *                                       *
  24.   *  IN  NO  EVENT  SHALL  SILICON  GRAPHICS  BE  LIABLE FOR ANY SPECIAL,  *
  25.   *  INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF  ANY  KIND,  OR ANY  *
  26.   *  DAMAGES WHATSOEVER RESULTING FROM LOSS  OF  USE,  DATA  OR  PROFITS,  *
  27.   *  WHETHER OR NOT ADVISED OF  THE  POSSIBILITY  OF  DAMAGE,  AND ON ANY  *
  28.   *  THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR  *
  29.   *  PERFORMANCE OF THIS SOFTWARE.                       *
  30.   **************************************************************************/
  31.  
  32. /*
  33.     Author : Patrick Bouchaud    & Paul Hansen
  34.          galaad@neu.sgi.com    hansen@engr.sgi.com
  35. */
  36.  
  37. #define    MAIN_PROGRAM
  38.  
  39. #include <sys/types.h>
  40. #include <unistd.h>
  41. #include <sys/stat.h>
  42. #include <fcntl.h>
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45. #include <math.h>
  46. #include <values.h>
  47. #include <string.h>
  48. #include <bstring.h>
  49. #include <sys/mman.h>
  50.  
  51. #include <GL/gl.h>
  52. #include <GL/glu.h>
  53. #include <GL/glut.h>
  54.  
  55. #include "data.h"
  56.  
  57. static int W, H, mousex, mousey;
  58. static float
  59.     ALTMIN, ALTMAX,
  60.     cos_head, sin_head,
  61.     cos_pitch, sin_pitch,
  62.     speed, delta_speed,
  63.     roll, pitch, head;
  64.  
  65. static void Redraw( void );
  66. static void Idle( void );
  67. static void Mouse( int, int, int, int );
  68. static void Motion( int, int );
  69. static void Reshape( int, int );
  70. static void Key( unsigned char, int, int );
  71.  
  72. static void InitTexture( void );
  73. static int ReadTerrain( char * );
  74. void InitializeRowData(int lat_stride, int lon_stride);
  75. void AfficheTerrain( int, int, int );
  76.  
  77. float obsLat, obsLon, obsAlt, texture_select;
  78.  
  79. int XCullDraw(
  80.     float altmin, float altmax,
  81.     int lat_stride, int lon_stride);
  82.  
  83. /*---------------------------------------------------------------------------*
  84.  *        MAIN                                 *
  85.  *---------------------------------------------------------------------------*/
  86. void
  87. main( int argc, char *argv[] )
  88. {
  89.     register int i;
  90.     float val;
  91.  
  92.     glutInit( &argc, argv );
  93.  
  94. /*
  95.     Local Initializations
  96.     ---------------------
  97. */
  98.     ReadTerrain( NULL );
  99.  
  100.     obsLat    = ((float) (NUM_LATITUDE/2))*D_LATITUDE;
  101.     obsLon    = ((float) (NUM_LONGITUDE/2))*D_LONGITUDE;
  102.     obsAlt    = 7000.0;
  103.     roll = pitch = head = 0;
  104.     speed = 0.;
  105.  
  106.     head = 0.0;
  107.     pitch = 60.0;
  108.  
  109.     delta_speed = 0.;
  110.     near_cull_dist = 10.;
  111.     far_cull_dist = 16000.;
  112.     
  113. /*
  114.     Graphics Initializations
  115.     ------------------------
  116. */
  117.     glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  118.     glutCreateWindow( "terrain following" );
  119.  
  120.     glutDisplayFunc( Redraw );
  121.     glutReshapeFunc( Reshape );
  122.     glutMouseFunc( Mouse );
  123.     glutIdleFunc(Idle);
  124.     glutPassiveMotionFunc( Motion );
  125.     glutMotionFunc( Motion );
  126.     glutKeyboardFunc(Key);
  127.  
  128.     glEnable( GL_DEPTH_TEST );
  129.     glEnable( GL_CULL_FACE );
  130.     glCullFace( GL_BACK );
  131.  
  132.     glMatrixMode(GL_PROJECTION);
  133.     glLoadIdentity();
  134.     gluPerspective( 60., 1., near_cull_dist, far_cull_dist );
  135.     glMatrixMode(GL_MODELVIEW);
  136.     glLoadIdentity();
  137.  
  138. #ifdef TEXTURED
  139.     InitTexture();
  140.     glShadeModel(GL_SMOOTH);
  141. #else 
  142.     glShadeModel(GL_FLAT);
  143. #endif
  144.  
  145.     InitializeRowData(LAT_STRIDE, LON_STRIDE);
  146.     glClearColor( .5, .5, 1., 1. );
  147.     glClearDepth( 1. );
  148.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  149.     glutSwapBuffers(); 
  150.  
  151.     glutMainLoop();
  152. }
  153.  
  154. /*---------------------------------------------------------------------------*
  155.  *    Redraw                                     *
  156.  *---------------------------------------------------------------------------*/
  157. static void
  158. Redraw( void )
  159. {
  160.     float aux, alt;
  161.  
  162.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  163.  
  164.     head  += (float)(2*mousex - W) / W;
  165.     pitch += (float)(H - 2*mousey) / H;
  166.  
  167. #ifdef DEV
  168.     fprintf( stderr, "\n\nobsLat = %f;\n"
  169.     "obsLon = %f;\nobsAlt = %f;\nhead = %f;\npitch = %f;\n\n",
  170.     obsLat, obsLon, obsAlt, head, pitch );
  171.     fprintf( stderr, "\nobsAlt = %f\n", obsAlt );
  172. #endif
  173.  
  174.     while (pitch < 0.) pitch += 360.;
  175.     while (pitch > 360.) pitch -= 360.;
  176.     while (head < 0.) head += 360.;
  177.     while (head > 360.) head -= 360.;
  178.  
  179.     aux = DEG2RAD((float) head);
  180.     cos_head = fcos( aux );
  181.     sin_head = fsin( aux );
  182.     aux = DEG2RAD((float) pitch);
  183.     cos_pitch = fcos( aux );
  184.     sin_pitch = fsin( aux );
  185.  
  186.     speed += delta_speed;
  187.     obsLat -= speed*cos_head*cos_pitch;
  188.     obsLon += speed*sin_head*cos_pitch;
  189.     obsAlt -= speed*sin_pitch;
  190.  
  191.     alt = Terrain[(int)(obsLat/D_LATITUDE)][(int)(obsLon/D_LONGITUDE)];
  192.     alt += RADADA;
  193.     obsAlt = SUP( alt, obsAlt );
  194.  
  195.     glMatrixMode(GL_PROJECTION);
  196.     glLoadIdentity();
  197.     gluPerspective( 60., 1., near_cull_dist, far_cull_dist );
  198.  
  199.     glMatrixMode(GL_MODELVIEW);
  200.     glLoadIdentity();
  201.     glRotatef( pitch, 1.0, 0.0, 0.0 );
  202.     glRotatef( head, 0.0, 1.0, 0.0 );
  203.     glTranslatef(-obsLon,-obsAlt,-obsLat);
  204.  
  205.     XCullDraw( ALTMIN, ALTMAX, LAT_STRIDE, LON_STRIDE);
  206.  
  207.     glutSwapBuffers(); 
  208. }
  209.  
  210. /*---------------------------------------------------------------------------*
  211.  *    Idle                                     *
  212.  *---------------------------------------------------------------------------*/
  213. static void
  214. Idle( void )
  215. {
  216.     glutPostRedisplay();
  217. }
  218.  
  219. /*---------------------------------------------------------------------------*
  220.  *    Mouse                                     *
  221.  *---------------------------------------------------------------------------*/
  222. static void
  223. Mouse( int button, int state, int x, int y )
  224. {
  225.     switch (button)
  226.     {
  227.     case GLUT_LEFT_BUTTON:
  228.         delta_speed = (state==GLUT_DOWN) ? 1. : 0.;
  229.         break;
  230.  
  231.     case GLUT_RIGHT_BUTTON:
  232.         delta_speed = (state==GLUT_DOWN) ? -1. : 0.;
  233.         break;
  234.  
  235.     case GLUT_MIDDLE_BUTTON:
  236.         speed = 0.;
  237.         delta_speed = 0.;
  238.         break;
  239.     }
  240. }
  241.  
  242. /*---------------------------------------------------------------------------*
  243.  *    Motion                                     *
  244.  *---------------------------------------------------------------------------*/
  245. static void
  246. Motion( int x, int y )
  247. {
  248.     mousex = x;
  249.     mousey = y;
  250. }
  251.  
  252. /*---------------------------------------------------------------------------*
  253.  *    Reshape                                     *
  254.  *---------------------------------------------------------------------------*/
  255. static void
  256. Reshape( int w, int h )
  257. {
  258.     glViewport( 0, 0, w, h );
  259.     W = w;
  260.     H = h;
  261.     mousex = W/2;
  262.     mousey = H/2;
  263. }
  264.  
  265. /*---------------------------------------------------------------------------*
  266.  *    Key                                     *
  267.  *---------------------------------------------------------------------------*/
  268. static void
  269. Key(unsigned char key, int x, int y)
  270. {
  271.     switch (key)
  272.     {
  273.     case 27: /* Escape */
  274.         exit(0);
  275.     }
  276. }
  277.  
  278. /*---------------------------------------------------------------------------*
  279.  *        ReadTerrain                                 *
  280.  *---------------------------------------------------------------------------*/
  281. static int
  282. ReadTerrain( char *filename )
  283. {
  284.     register int i;
  285.     register float *altitude;
  286.     FILE *fp;
  287.  
  288.     fp = fopen("data/elevator.bin", "rb");
  289.     fread(&Terrain[0][0], 1, NUM_LATITUDE*NUM_LONGITUDE*4, fp);
  290.  
  291.     ALTMAX = ALTMIN = Terrain[0][0];
  292.  
  293.     for (i=0, altitude = &Terrain[0][0]; i<NUM_LATITUDE*NUM_LONGITUDE; i++, altitude++) {
  294.     ALTMAX = SUP( ALTMAX, *altitude );
  295.     ALTMIN = INF( ALTMIN, *altitude );
  296.     }
  297.     fclose( fp );
  298. }
  299.  
  300. /*---------------------------------------------------------------------------* 
  301.  *    InitTexture -- the image data is read from a file called "cooked.bin".
  302.  *     the data is "cooked" in the sense that it is highly processed: tiled
  303.  *     and interlaced to support the texture-select extension for Impact
  304.  *     Graphics systems with the TRAM option card. The original image 
  305.  *     (6000x6000) is placed in the lower-left of a 6Kx6K array; smaller
  306.  *     mipmaps are generated and, for each mipmap level, tiles, including
  307.  *     border data, are extracted and interlaced in groups of 4 "subtiles".
  308.  *
  309.  *     Thus four luminance subtiles get grouped as a single 4-channel
  310.  *     (GL_RGBA) "quad-selectable". All of the tiles for one mipmap level
  311.  *     are concatenated in "cooked.bin", followed by all the tiles for
  312.  *     successively smaller levels.
  313.  *---------------------------------------------------------------------------*/
  314. #ifdef TEXTURED
  315.  
  316. static void
  317. InitTexture( void )
  318. {
  319.     int i, j, k, lev, tile, subtiles, fptr;
  320.     FILE *fp;
  321.     size_t size, tile_width=TILE_WIDTH, tile_height=TILE_HEIGHT;
  322.     GLenum internalformat, externalformat, externaltype;
  323.     GLubyte *Texture;
  324.  
  325.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  326.     glEnable( GL_TEXTURE_2D );
  327.  
  328.     /* We use the proxy-texture extension to test whether the
  329.        texture-select extension is supported. If it is, there
  330.        are 144 quad-selectable textures; if not, there are
  331.        576 luminance textures.                   */
  332.  
  333.     glTexImage2D(GL_PROXY_TEXTURE_2D_EXT, 0, GL_QUAD_LUMINANCE8_SGIS, 1, 1, 0,
  334.             GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  335.     texture_select = (int)(glGetError() == GL_NO_ERROR);
  336.  
  337.     /* NOTE: in the case where the texture-select extension is not
  338.        supported, we "work around" the fact that the data is
  339.        interlaced by declaring it to be unsigned int's. These
  340.        int's have valid data (for this pass) in the MS byte, which
  341.        are converted to bytes by the system during download. Then
  342.        we offset by one byte and go through the data again (4 times total). */
  343.  
  344.     fp = fopen("./data/cooked.bin", "rb");
  345.     fprintf(stderr, "lecture de %s: ...", "cooked.bin");
  346.  
  347.     if(texture_select) {
  348.     subtiles = 1;
  349.     internalformat = GL_QUAD_LUMINANCE8_SGIS;
  350.     externalformat = GL_RGBA;     /* actually, 4 luminance channels */
  351.     externaltype = GL_UNSIGNED_BYTE;
  352.     }
  353.     else {
  354.     subtiles = 4;
  355.     internalformat = GL_LUMINANCE8_EXT;
  356.     externalformat = GL_LUMINANCE;
  357.     externaltype = GL_UNSIGNED_INT;     /* system takes top 8 bits */
  358.     fprintf(stderr, "\ntexture-select not supported on this system.");
  359.     fprintf(stderr, "\nextra processing time required ...");
  360.     }
  361.  
  362.     Texture = (GLubyte*)malloc((TILE_WIDTH+2)*(TILE_HEIGHT+2)*4+3);
  363.     for(lev=fptr=0; lev<MAXLEV; ++lev, tile_width>>=1, tile_height>>=1) {
  364.     size = (tile_width+2)*(tile_height+2)*4;
  365.     for(i=0, tile=1; i<NUMTILE_S; ++i) {
  366.         for(j=0; j<NUMTILE_T; ++j) {
  367.         fread(Texture, size, 1, fp);
  368.         for(k=0; k<subtiles; ++k, tile++) {
  369.             glBindTextureEXT(GL_TEXTURE_2D, tile);
  370.             if(lev == 0) {
  371.                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  372.                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  373.                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  374.                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  375.                             GL_LINEAR_MIPMAP_LINEAR);
  376.             }
  377.             glTexImage2D(
  378.             GL_TEXTURE_2D,    /* target */
  379.             lev,        /* level (mip-mapping) */
  380.             internalformat,    /* INTERNAL format */
  381.             tile_width+2,    /* texture level width */
  382.             tile_height+2,    /* texture level height */
  383.             1,            /* border */
  384.             externalformat,    /* EXTERNAL format */
  385.             externaltype,    /* EXTERNAL type */
  386.             Texture+k        /* data (k=byte offset, see NOTE above) */
  387.             );
  388.             }
  389.         }
  390.         }
  391.     }
  392.  
  393.     fclose(fp);
  394.     free(Texture);
  395.     fprintf(stderr, "Done\n");
  396.     glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  397.     glMatrixMode( GL_MODELVIEW );
  398. }
  399. #endif    /* TEXTURED */
  400.